package com.sensePlusVR.realGame;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class VideoGameController {
	private static final String TAG = "VideoGameController";
	public static final int IDLE = 0;
	public static final int LOW = 1;
	public static final int MID = 2;
	public static final int HEIGH = 3;
	
	private static final int HeighSpeed = 8;
	private static final int MidSpeed   = 13;
	//private CpuMediaInfo.DecoderRes mDres;
	//private List<RealGameConfig> mGameList;
	private List<SimapleAction> mCurrentActions;
	private RealGameConfig mCurrentGame;
	//private int mCurrentGameIndex;
	private int mCurrentActionMinor;
	private int mCurrentActionMain;
	private int mCurrentActionFov;
	private int mActionsNr = 0;
//	
//	public VideoGameController(List<String> configfiles){
//		if (configfiles.size() == 0)
//			return;
//		mGameList = new ArrayList<RealGameConfig>();
//		for (String s:configfiles){
//			RealGameConfig gameconfig = new RealGameConfig(s);
//			mGameList.add(gameconfig);
//		}
//		mCurrentGameIndex = 0;
//		mCurrentGame = mGameList.get(mCurrentGameIndex);
//		mCurrentActionMain = 0;
//		mCurrentActions = getActions(mCurrentActionMain);
//	}
		
	public VideoGameController(String configfile){
		Log.e(TAG, "configfile="+configfile);
		mCurrentGame = new RealGameConfig(configfile);
		mCurrentActionMain = 0;
		mCurrentActionFov = 0;
		mCurrentActions = getActions(mCurrentActionMain);
		mActionsNr = mCurrentGame.getActionsNumber();
		//mDres = CpuMediaInfo.getDecoderLimite();
	}

	public int getActionVideoWidth(){
		if (mCurrentGame != null)
			return mCurrentGame.getActionVideoWidth();
		return 0;
	}
	
	public int getActionVideoHeight(){
		if (mCurrentGame != null)
			return mCurrentGame.getActionVideoHeight();
		return 0;
	}
	
	public int getSourceType()
	{
		if (mCurrentGame != null)
			return mCurrentGame.getSourceType();
		return 0;
	}
	
	public int getConfigVersion()
	{
		if (mCurrentGame != null)
			return mCurrentGame.getConfigVersion();
		return 0;
	}
	
	
	public void LastAction(){
		mCurrentActionMain = mActionsNr - 1;
		mCurrentActions = getActions(mCurrentActionMain);
		mCurrentActionMinor = 0;
	}
	
	public void NextAction(){
		Log.e(TAG, "NextAction");
		mCurrentActionMain = (mCurrentActionMain + 1) % mActionsNr;
		mCurrentActions = getActions(mCurrentActionMain);
		mCurrentActionMinor = 0;
		Log.e(TAG, "NextAction end");
	}
	
	public boolean currentSupportFov(){
		for (SimapleAction a:mCurrentActions){
			if (a.getFov() == 1)//Fov 0,1,2,3...
				return true;
		}
		return false;
	}
	
	public void NextFov(){
		mCurrentActionFov++;
		mCurrentActions = getActions(mCurrentActionMain);
		if (mCurrentActions == null || mCurrentActions.size() == 0){
			mCurrentActionFov = 0;
			mCurrentActions = getActions(mCurrentActionMain);
		}
		mCurrentActionMinor = 0;		
	}
	
	public void ChangeAction(){
		Log.e(TAG, "ChangeAction");
		mCurrentActionFov++;
		mCurrentActions = getActions(mCurrentActionMain);
		if (mCurrentActions.size() == 0){
			mCurrentActionFov = 0;
			NextAction();
		}
		mCurrentActionMinor = 0;
	}
	
	public String getStartVideo(){
		return mCurrentGame.getStartActionVideo();
	}
	
	public String getEndVideo(){
		return mCurrentGame.getEndActionVideo();
	}
	
	public String getBackAudioFile(){
		String file = null;
		if (mCurrentGame.getBackAudio() != null){
			Log.e(TAG, "get backaudio=?");
			file = mCurrentGame.getBackAudio().getRandomAudio();
		}
		Log.e(TAG, "backaudio="+file);
		return file;
	}
	
	private List<SimapleAction> getActions(int index){
		List<SimapleAction> list = mCurrentGame.getActions(index);
		List<SimapleAction> ret = new ArrayList<SimapleAction>();
		for (SimapleAction a:list){
			if (a.getFov() == mCurrentActionFov){
				ret.add(a);
			}
		}
		return ret;
	}
	
	public void setCurrentMinor(int minor){
		if (minor < mCurrentActions.size())
			mCurrentActionMinor = minor;
		else
			mCurrentActionMinor = 0;
	}
	
	public String getCurrentAction(){
		String actions = null;
		for (SimapleAction a: mCurrentActions){
			if (actions == null){
				actions = a.toString();
				if (actions == null)
					return null;
			}else {
				actions = actions + ";" + a.toString();
			}
		}
		return actions;
	}
	
	public String getCurrentDesVideo()
	{
		String desVideo = null;
		for (SimapleAction a:mCurrentActions)
		{
			if (a != null)
			{
				desVideo = a.getmDesVideo();
				break;
			}
		}
		return desVideo;
	}
	
	public int getStep(int speed){
		if (speed == 0)
			return IDLE;
		else if (speed <= HeighSpeed)
			return HEIGH;
		else if (speed <= MidSpeed)
			return MID;
		else
			return LOW;
	}
	
	
	public String getAudioScreams(int speed){
		SimapleAction minor = null;
		for (SimapleAction a: mCurrentActions){
			if (a.getMinorAction() == mCurrentActionMinor){
				minor = a;
			}
		}
		if (minor == null)
			return null;		
		int step = getStep(speed);
		String file = null;
		if (step == IDLE)
			file = minor.getmAudioIdle();
		else if (step == HEIGH)
			file = minor.getmAudioHeigh();
		else if (step == MID)
			file = minor.getmAudioMid();
		else
			file = minor.getmAudioLow();
		//Log.e(TAG, "getaudio file="+file);
		return file;
	}
	
	public String getSound(int step){
		SimapleAction minor = null;
		for (SimapleAction a: mCurrentActions){
			if (a.getMinorAction() == mCurrentActionMinor){
				minor = a;
			}
		}
		if (minor == null)
			return null;
		String file = null;
		if (step == IDLE)
			file = minor.getmAudioIdle();
		else if (step == HEIGH)
			file = minor.getmAudioHeigh();
		else if (step == MID)
			file = minor.getmAudioMid();
		else
			file = minor.getmAudioLow();
		//Log.e(TAG, "getaudio file="+file);
		return file;
	}
	
	public boolean supportVideo(){
		return mCurrentGame.supportVideo();
	}
}
